<template>
  <div class="container">
    <div class="btnClose">
      <router-link to="/" class="iconfont iconicon-test"></router-link>
    </div>
    <div class="logo">
      <span class="iconfont iconnew"></span>
    </div>
    <!-- 输入框组件 -->
    <MyInput
      :rule="/^\w{3,12}$/"
      type="text"
      placeholder="用户名 | 手机号码"
      ellMsg="请输入正确的用户名"
      @setValue="setUsername"
    />
    <MyInput
      :rule="/^\d{3,12}$/"
      type="password"
      placeholder="密码"
      ellMsg="密码仅限6至12的数字"
      @setValue="setPassword"
    />
    <!-- 按钮组件 -->
    <!-- .native - 监听组件根元素的原生事件 -->
    <MyButton value="登录" @click.native="login" />
  </div>
</template>

<script>
//组件引入
import MyInput from "@/components/MyInput";
import MyButton from "@/components/MyButton";
export default {
  components: {
    MyInput,
    MyButton,
  },
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    setUsername(newValue) {
      this.username = newValue;
    },
    setPassword(newValue) {
      this.password = newValue;
    },
    login() {
      this.$axios({
        method: "post",
        url: "/login",
        data: {
          username: this.username,
          password: this.password,
        },
      }).then((res) => {
        console.log(res.data);
        if (res.data.message === "登录成功") {
          // 登录成功保存token与用户的id
          localStorage.setItem("token", res.data.data.token);
          localStorage.setItem("userId", res.data.data.user.id);
          this.$toast(res.data.message);
          this.$router.push("/");
          // 因为无论如何都返回 200 响应码 总是会走成功的逻辑, 这种风格的错误, 只会在返回的数据层面上报错,
          // 只能在 .then 成功逻辑上面进行处理
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 24/360 * 100vw;
}
.btnClose {
  .iconfont {
    font-size: 28/360 * 100vw;
  }
}
.logo {
  text-align: center;
  .iconfont {
    font-size: 126/360 * 100vw;
    color: #d81e06;
  }
}
</style>